<template>
  <div class="login-container">
    <div class="login-form">
      <div class="login-header">
        <div class="system-logo">
          <el-icon size="40" color="#409eff"><Lock /></el-icon>
        </div>
        <h2>作业许可管理系统</h2>
        <p>安全作业，规范管理</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form-content"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="account">
          <el-input
            v-model="loginForm.account"
            placeholder="工号/手机号"
            size="large"
            prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <div class="login-options">
            <el-checkbox v-model="loginForm.remember">
              记住账号
            </el-checkbox>
            <el-link type="primary" :underline="false">
              忘记密码？
            </el-link>
          </div>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
        
        <el-form-item v-if="showIntegratedLogin">
          <el-divider>或</el-divider>
          <el-button
            size="large"
            @click="handleIntegratedLogin"
            class="integrated-login-button"
          >
            <el-icon><Connection /></el-icon>
            集成认证登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <p>测试账号：admin / 123456</p>
        <p>工号登录：10001 / 123456</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { useUserStore } from '@/stores/user'

interface LoginForm {
  account: string
  password: string
  remember: boolean
}

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref<FormInstance>()
const loading = ref(false)
const showIntegratedLogin = ref(false) // 是否显示集成认证登录

const loginForm = reactive<LoginForm>({
  account: 'admin',
  password: '123456',
  remember: false
})

const loginRules: FormRules = {
  account: [
    { required: true, message: '请输入工号或手机号', trigger: 'blur' },
    { min: 2, max: 20, message: '账号长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

// 加载记住的账号
onMounted(() => {
  const rememberedAccount = localStorage.getItem('rememberedAccount')
  if (rememberedAccount) {
    loginForm.account = rememberedAccount
    loginForm.remember = true
  }
})

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    // 处理记住账号
    if (loginForm.remember) {
      localStorage.setItem('rememberedAccount', loginForm.account)
    } else {
      localStorage.removeItem('rememberedAccount')
    }
    
    // 转换登录表单格式
    const loginData = {
      username: loginForm.account,
      password: loginForm.password
    }
    
    await userStore.loginAction(loginData)
    ElMessage.success('登录成功')
    router.push('/')
  } catch (error: any) {
    ElMessage.error(error.message || '登录失败')
  } finally {
    loading.value = false
  }
}

const handleIntegratedLogin = () => {
  ElMessage.info('集成认证登录功能开发中...')
}
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-form {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
  
  .system-logo {
    margin-bottom: 16px;
  }
  
  h2 {
    color: #333;
    margin-bottom: 8px;
    font-size: 24px;
    font-weight: 600;
  }
  
  p {
    color: #666;
    font-size: 14px;
  }
}

.login-form-content {
  .el-form-item {
    margin-bottom: 20px;
  }
  
  .login-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
  }
  
  .login-button {
    width: 100%;
    height: 44px;
    font-size: 16px;
  }
  
  .integrated-login-button {
    width: 100%;
    height: 44px;
    font-size: 16px;
    border: 1px solid #dcdfe6;
    background: #fff;
    color: #606266;
    
    &:hover {
      border-color: #409eff;
      color: #409eff;
    }
  }
}

.login-footer {
  text-align: center;
  margin-top: 20px;
  
  p {
    color: #999;
    font-size: 12px;
  }
}

:deep(.el-input__wrapper) {
  height: 44px;
}
</style>
